package com.lrkj.property.service.impl;

import com.lrkj.common.constant.OrgConstants;
import com.lrkj.common.utils.DateUtils;
import com.lrkj.common.utils.SecurityUtils;
import com.lrkj.common.utils.uuid.IdUtils;
import com.lrkj.property.domain.Carport;
import com.lrkj.property.domain.Parking;
import com.lrkj.property.mapper.CarportMapper;
import com.lrkj.property.service.ICarportService;
import com.lrkj.property.service.IParkingService;
import com.lrkj.system.service.IOrgTreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 车位管理Service业务层处理
 *
 * @author yajun_ren
 * @date 2022-03-15
 */
@Service
public class CarportServiceImpl implements ICarportService {
    @Autowired
    private CarportMapper carportMapper;

    @Autowired
    private IOrgTreeService orgTreeService;

    @Autowired
    private IParkingService parkingService;

    /**
     * 查询车位管理
     *
     * @param carportId 车位管理ID
     * @return 车位管理
     */
    @Override
    public Carport selectCarportById(String carportId) {
        return carportMapper.selectCarportById(carportId);
    }

    /**
     * 查询车位管理列表
     *
     * @param carport 车位管理
     * @return 车位管理
     */
    @Override
    public List<Carport> selectCarportList(Carport carport) {
        return carportMapper.selectCarportList(carport);
    }


    /**
     * 新增车位管理
     *
     * @param carport 车位管理
     * @return 结果
     */
    @Override
    public int insertCarport(Carport carport) {
        carport.setCarportId(IdUtils.simpleUUID());
        carport.setCreateBy(SecurityUtils.getUsername());
        carport.setCreateTime(DateUtils.getNowDate());
        this.setUnitInfo(carport);
        return carportMapper.insertCarport(carport);
    }

    /**
     * 修改车位管理
     *
     * @param carport 车位管理
     * @return 结果
     */
    @Override
    public int updateCarport(Carport carport) {
        carport.setUpdateBy(SecurityUtils.getUsername());
        carport.setUpdateTime(DateUtils.getNowDate());
        this.setUnitInfo(carport);
        return carportMapper.updateCarport(carport);
    }

    /**
     * 批量删除车位管理
     *
     * @param carportIds 需要删除的车位管理ID
     * @return 结果
     */
    @Override
    public int deleteCarportByIds(String[] carportIds) {
        orgTreeService.deleteOrgTreeByIds(carportIds);
        return carportMapper.deleteCarportByIds(carportIds);
    }

    /**
     * 删除车位管理信息
     *
     * @param carportId 车位管理ID
     * @return 结果
     */
    @Override
    public int deleteCarportById(String carportId) {
        orgTreeService.deleteOrgTreeById(carportId);
        return carportMapper.deleteCarportById(carportId);
    }

    private void setUnitInfo(Carport bean) {
        Parking parentBean = parkingService.selectParkingById(bean.getParkingId());
        bean.setAncestors(parentBean.getAncestors() + "," + parentBean.getParkingId());
        bean.setDeptId(parentBean.getDeptId());
        orgTreeService.insertOrgTree(bean.getCarportId(), bean.getCarportName(), parentBean.getParkingId(), OrgConstants.carp, parentBean.getAncestors(),bean.getOrderNum());
    }

    /**
     * 批量新增车位管理信息
     *
     * @param carports 车位管理对象
     */
    public int batchAddData(List<Carport> carports) {
        return carportMapper.batchAddData(carports);
    }
}
